package insert

import (
	"context"

	"server_1.0/api/internal/svc"
	"server_1.0/api/internal/types"
	"server_1.0/service/major/major"
	"server_1.0/util"

	"github.com/zeromicro/go-zero/core/logx"
)

type InsertMajorLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewInsertMajorLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertMajorLogic {
	return &InsertMajorLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InsertMajorLogic) InsertMajor(req *types.MajInsertReq) (resp *types.PostReps, err error) {
	code := util.Success
	Msg := "成功"

	ins, _ := l.svcCtx.MajorRpc.Insert(l.ctx, &major.InsertReq{
		No:   req.Mno,
		ID:   req.Mid,
		Name: req.Mname,
	})
	if ins.Count == 0 {
		code = util.AddError
		Msg = "失败"
	}
	return &types.PostReps{
		Code: code,
		Msg:  Msg,
		Data: map[string]interface{}{
			"res": ins.QRs,
		},
	}, nil
}
